<template>
    <ul class="navs">
        <li class="home"><RouterLink to="/">首页</RouterLink></li>
        <li
            v-for="list in lists"
            :key="list.id"
            @mouseenter="debounce(useCategoryStore.show(list), 1000)"
            @mouseleave="debounce(useCategoryStore.hide(list), 1000)"
            @click="debounce(useCategoryStore.hide(list), 1000)"
        >
            <router-link :to="`/category/${list.id}`">{{
                list.name
            }}</router-link>

            <div class="layer" :class="{ open: list.open }">
                <ul>
                    <li
                        v-for="sub in list.sub"
                        :key="sub.children_id"
                        @click="useCategoryStore.hide(list)"
                    >
                        <router-link :to="`/category/sub/${sub.children_id}`">
                            <el-image
                                :src="sub.children_picture"
                                :alt="sub.children_name"
                            />
                            <p>{{ sub.children_name }}</p>
                        </router-link>
                    </li>
                </ul>
            </div>
        </li>
    </ul>
</template>

<script setup>
import { categoryStore } from '@/stores/category'
import { computed } from 'vue'
import { debounce } from '@/hooks/home'
const useCategoryStore = categoryStore()
const lists = computed(() => useCategoryStore.list)
</script>

<style lang="scss" scoped>
.navs {
    width: 1220px;
    display: flex;
    justify-content: space-around;
    padding-left: 20px;
    li {
        padding: 10px;
        width: 60px;
        text-align: center;
        a {
            display: flex;
            justify-content: center;
            align-items: center;
            font-size: 14px;
            line-height: 32px;
            height: 32px;
            display: inline-block;
            transition: color 0.3s ease;
            white-space: nowrap;
        }
        &:hover {
            > a {
                color: #da9233;
                border-bottom: 1px solid #da9233;
            }
        }
        &:nth-last-child(1) {
            padding-right: 20px;
        }
    }
}
.layer {
    margin-left: 220px;
    z-index: 100;
    width: 1200px;
    background-color: #fff;
    position: absolute;
    left: -200px;
    top: 64px;
    height: 0;
    visibility: hidden;
    opacity: 0;
    box-shadow: 0 0 5px #ccc;
    transition: all 0.2s ease;
    &.open {
        height: 132px;
        opacity: 1;
        visibility: visible;
    }
    ul {
        display: flex;
        flex-wrap: wrap;
        padding: 0 70px;
        align-items: center;
        height: 132px;
        li {
            width: 110px;
            text-align: center;
            @include hoverShadow;
            .el-image {
                width: 100%;
                height: 60px;
            }
            p {
                padding-top: 10px;
                transition: color 0.2s ease;
            }
            &:hover {
                p {
                    color: #da9233;
                }
            }
        }
    }
}
</style>
